package com.zr.wareHouseIdentify.Controller;

import com.zr.meiju.StatusEnum;
import com.zr.meiju.XiaLaVo;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import com.zr.wareHouseIdentify.Service.WareHouseService;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentify;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentifyAdd;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentifyUpdate;
import com.zr.wareHouseIdentify.pojo.WareHouseStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiangping on 2019/7/16.
 */
@RestController
@RequestMapping("wareHouseIdentify/")
public class WareHouseController {
    @Autowired
    private WareHouseService wareHouseService;
    /**
     *新增
     *
     **/
    @PostMapping("warehouse/add")
    @CrossOrigin
    public ResultVo addWarehouse(@RequestBody WareHouseIdentifyAdd wareHouseIdentifyAdd, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return wareHouseService.addWarehouse(wareHouseIdentifyAdd);
    }
    /**
     *新增
     *
     **/
    @PostMapping("warehouse/update")
    @CrossOrigin
    public ResultVo UpdateWarehouse(@RequestBody WareHouseIdentifyUpdate wareHouseIdentifyUpdate, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return wareHouseService.UpdateWarehouse(wareHouseIdentifyUpdate);
    }
    /*
    *
    * 状态下拉框
    * */
    @GetMapping("warehouse/start")
    public ResultVo<List<XiaLaVo>> enquiryStatus(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< StatusEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setValue(i);
            xiaLaVo.setLabel(StatusEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }
    /**
     * 查询
     */
    /**
     * 分页查询
     */
    @PostMapping("warehouse/queryPage")
    public ResultVo queryPage(@RequestBody  WareHouseIdentify wareHouseIdentify){

        return wareHouseService.queryPage(wareHouseIdentify);
    }
    /**
     * 修改状态接口
     * @param bindingResult
     * @return
     */
    @PostMapping("subClass/updateStatus")
    public ResultVo updateStatus(@RequestBody WareHouseStatusVo wareHouseStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return wareHouseService.updateStatus(wareHouseStatusVo);
    }
}
